#include "tabwidget.h"

Tabwidget::Tabwidget(QPushButton* u, QPushButton* a, bool u_a) : user(u), amm(a), user_amm(u_a)
{
    QVBoxLayout* mainLayout = new QVBoxLayout;

    // crea menu
    QMenuBar* menuBar = new QMenuBar(0);
    QMenu* fileMenu = menuBar->addMenu(tr("File"));
    QAction* exitAction = fileMenu->addAction(tr("Close"));

    QMenu* viewMenu = menuBar->addMenu(tr("&View"));
    fullScreenAction = viewMenu->addAction(tr("Full screen"));
    normalViewAction = viewMenu->addAction(tr("Normal view"));

    if(this->isFullScreen() || this->isMaximized()) fullScreenAction->setEnabled(true);
    else normalViewAction->setEnabled(true);

    // connessione signal-slot
    connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));  //richiama il closeEvent di tabbed, modificato sotto
    connect(fullScreenAction, SIGNAL(triggered()), this, SLOT(showFullScreen()));
    connect(normalViewAction, SIGNAL(triggered()), this, SLOT(showNormal()));

    mainLayout->setMenuBar(menuBar);

    setLayout(mainLayout);

    setTabPosition(QTabWidget::South);
    showFullScreen();
}

void Tabwidget::closeEvent(QCloseEvent *event){
    if(!user_amm){//user_amm = 0 = user
        if(!(user->isEnabled())) user->setEnabled(true);    //se user non e' abilitato,alla chiusura lo riabilito
       }

    else            //user_amm = 1 = amm
        if(!(amm->isEnabled())) amm->setEnabled(true);      //se amm non e' abilitato,alla chiusura lo riabilito

    for(int i=0; i<this->count(); i++){ //elimino garbage, le tab di una tabbed
        delete this->widget(i);
    }

    event->accept(); //in ogni caso chiudo!
    delete this;    //elimino anche tabbed
}

void Tabwidget::showFullScreen(){
    QTabWidget::showFullScreen();
    fullScreenAction->setEnabled(false);
    normalViewAction->setEnabled(true);
}

void Tabwidget::showNormal(){
    QTabWidget::showNormal();
    normalViewAction->setEnabled(false);
    fullScreenAction->setEnabled(true);
    setGeometry(0,0,650,700);   //do una grandezza definita,senno' mi si apre piccolissima
}
